package com.jiutong.meipengdai.ui.activitys;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.jiutong.meipengdai.R;
import com.jiutong.meipengdai.base.BaseActivity;
import com.jiutong.meipengdai.base.CommonAdapter;
import com.jiutong.meipengdai.base.ViewHolder;
import com.jiutong.meipengdai.common.Constants;
import com.jiutong.meipengdai.entity.BankCardListRespond;
import com.jiutong.meipengdai.entity.BaseRespond;
import com.jiutong.meipengdai.http.model.interfaces.BankListBiz;
import com.jiutong.meipengdai.http.model.interfaces.CheckLoginInterface;
import com.jiutong.meipengdai.http.present.BankListPresent;
import com.jiutong.meipengdai.http.subscribers.SubscriberOnNextListener;
import com.jiutong.meipengdai.utils.SpConfig;
import com.jiutong.meipengdai.views.dialog.BaseDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by suxi on 2017/6/2.
 */

@CheckLoginInterface
public class BankCardListActivity extends BaseActivity implements BankListBiz {
    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.lv_bankcards)
    ListView lvBankcards;
    @Bind(R.id.tv_add_bankcard)
    TextView tvAddBankcard;
    @Bind(R.id.ll_nobanks)
    LinearLayout llNobanks;
    private BankCardListAdapter bankCardListAdapter;
    private List<BankCardListRespond.DataBean> bankInfos;
    private BankListPresent bankListPresent;
    private BaseDialog authDialog;
    private BaseDialog dialog;

    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_bankcard_list;
    }

    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);
        tvTitle.setText("我的银行卡");
    }

    @Override
    protected void initDate(Bundle savedInstanceState) {
        super.initDate(savedInstanceState);
        bankListPresent = new BankListPresent(this, this);
        bankInfos = new ArrayList<BankCardListRespond.DataBean>();
    }

    @Override
    protected void onResume() {
        super.onResume();
        bankInfos.clear();
        bankListPresent.getBanklist();
    }

    @OnClick({R.id.iv_back, R.id.tv_add_bankcard})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_add_bankcard:
                if (!SpConfig.getInstance().getBool(Constants.ISAUTH_BOOLEAN)) {
                    showAutoDialog();
                    break;
                }
                openActivity(AddBankCardActivity.class);
                break;
        }
    }


    @Override
    public void onSuccess(Object object) {
        BankCardListRespond respond = (BankCardListRespond) object;

        if (!"200".equals(respond.getResCode())) {
            toastShortShow(respond.getResDesc());
            return;
        }

        if (respond.getData().size() < 1) {
            llNobanks.setVisibility(View.VISIBLE);
            lvBankcards.setVisibility(View.GONE);
            SpConfig.getInstance().putBool(Constants.BANKSTATE_BOOLEAN, false);
        } else {
            llNobanks.setVisibility(View.GONE);
            lvBankcards.setVisibility(View.VISIBLE);
            SpConfig.getInstance().putBool(Constants.BANKSTATE_BOOLEAN, true);
            bankInfos.addAll(respond.getData());
        }

        if (bankCardListAdapter == null) {
            bankCardListAdapter = new BankCardListAdapter(mContext, bankInfos, R.layout.view_bankcard_item);
            lvBankcards.setAdapter(bankCardListAdapter);
        } else {
            bankCardListAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onError(Throwable e) {

    }

    @Override
    public String getBankNumber() {
        return null;
    }

    @Override
    public String getPhoneNo() {
        return null;
    }

    @Override
    public String getBankId() {
        return null;
    }

    private void showAutoDialog() {
        if (authDialog == null) {

            authDialog = new BaseDialog.Builder()
                    .layResuorce(R.layout.dialog_to_auto)
                    .animResource(R.style.dialog_animation_fade)
                    .okViewClickEvent(R.id.tv_goauth)
                    .outSideCancel(true)
                    .gravityStyle(Gravity.CENTER)
                    .build();

            authDialog.setOnDialogClickListener(new BaseDialog.OnDialogClickListener() {
                @Override
                public void onOkClick() {
                    openActivity(RealNameActivity.class);
                    authDialog.dismiss();
                }

                @Override
                public void onOk2Click() {

                }
            });
        }

        authDialog.show(getSupportFragmentManager(), TAG);
    }

    private void showUnbindDialog(final String noagree) {
        if (dialog == null) {

            dialog = new BaseDialog.Builder().layResuorce(R.layout.dialog_bankcard_unbind)
                    .okViewClickEvent(R.id.tv_ok)
                    .cancelViewClickEvent(R.id.tv_cancel)
                    .dialogWidth(-2)
                    .gravityStyle(Gravity.CENTER)
                    .animResource(R.style.dialog_animation_fade)
                    .build();
        }

        dialog.setOnDialogClickListener(new BaseDialog.OnDialogClickListener() {
            @Override
            public void onOkClick() {
                dialog.dismiss();
                SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<BaseRespond>() {
                    @Override
                    public void onNext(BaseRespond subjects) {
                        if ("200".equals(subjects.getResCode())) {
//                            finish();
                            bankInfos.clear();
                            bankListPresent.getBanklist();
                        } else {
                            toastShortShow(subjects.getResDesc());
                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                    }
                };

                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("noAgree", noagree);

                bankListPresent.delBannkInfo(onNextListener, hashMap);
            }

            @Override
            public void onOk2Click() {

            }
        });

        if (!dialog.isVisible())
            dialog.show(getSupportFragmentManager(), TAG);
    }

    public class BankCardListAdapter extends CommonAdapter<BankCardListRespond.DataBean> {

        public BankCardListAdapter(Context context, List<BankCardListRespond.DataBean> listDatas, int layoutId) {
            super(context, listDatas, layoutId);
        }

        @Override
        protected void fillData(ViewHolder holder, int position) {
            TextView tv_bank_name = holder.getView(R.id.tv_bank_name);
            TextView tv_subbranch = holder.getView(R.id.tv_subbranch);
            TextView tv_bank_num = holder.getView(R.id.tv_bank_num);
            ImageView iv_bank_icon = holder.getView(R.id.iv_bank_icon);
//            LinearLayout llContaner = holder.getView(R.id.ll_contaner);
            Button btn_unbind = holder.getView(R.id.btn_unbind);

            btn_unbind.setTag(listDatas.get(position).getNoAgree());
            btn_unbind.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String tag = (String) v.getTag();
                    showUnbindDialog(tag);
                }
            });

            tv_bank_name.setText(listDatas.get(position).getBankName());
//            tv_subbranch.setText(listDatas.get(position).getSubbranch());
            String bankNumber = listDatas.get(position).getBankNo();

            String bankCode = listDatas.get(position).getBankCode();
            switch (bankCode) {
                case "01030000"://农业银行
                    iv_bank_icon.setImageResource(R.mipmap.nonye_bank);
                    break;

                case "03010000"://交通银行
                    iv_bank_icon.setImageResource(R.mipmap.jiaotong_bank);
                    break;

                case "01020000"://工商
                    iv_bank_icon.setImageResource(R.mipmap.icbc_bank);
                    break;

                case "01000000"://邮政
                    iv_bank_icon.setImageResource(R.mipmap.youzheng_bank);
                    break;

                case "03060000"://广发
                    iv_bank_icon.setImageResource(R.mipmap.guangfa_bank);
                    break;

                case "03070000"://平安
                    iv_bank_icon.setImageResource(R.mipmap.pingan_bank);
                    break;

                case "03080000"://招商
                    iv_bank_icon.setImageResource(R.mipmap.zhaoshang_bank);
                    break;

                case "03050000"://民生
                    iv_bank_icon.setImageResource(R.mipmap.minsheng_bank);
                    break;

                case "01040000"://中国
                    iv_bank_icon.setImageResource(R.mipmap.zhongguo_bank);
                    break;

                case "01050000"://建设
                    iv_bank_icon.setImageResource(R.mipmap.cbc_bank);
                    break;

                case "03030000"://兴业
                    iv_bank_icon.setImageResource(R.mipmap.xingye_bank);
                    break;

                case "03020000"://中信
                    iv_bank_icon.setImageResource(R.mipmap.zhongxin_bank);
                    break;

                case "03040000"://华夏
                    iv_bank_icon.setImageResource(R.mipmap.huaxia_bank);
                    break;

                case "03100000"://浦发
                    iv_bank_icon.setImageResource(R.mipmap.pufa_bank);
                    break;

                default:
                    iv_bank_icon.setImageResource(R.mipmap.bank_card_gray_icon);
                    break;
            }
//            String nameOfBank = BankCheck.getNameOfBank(bankNumber);
//            String[] strings = nameOfBank.split("·");

            tv_bank_num.setText(bankNumber.substring(bankNumber.length() - 4));
        }
    }
}
